package world.snowcrystal.datasource;

import org.springframework.stereotype.Component;
import world.snowcrystal.model.enums.SearchTypeEnum;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源注册器
 *
 *  
 */
@Component
public class DataSourceRegistry {

    @Resource
    private EsPostDataSource esPostDataSource;

    @Resource
    private SqlUserDataSource sqlUserDataSource;

    @Resource
    private BingPictureDataSource bingPictureDataSource;

    private Map<String, DataSource<?>> typeDataSourceMap;

    @PostConstruct
    public void doInit() {
        typeDataSourceMap = new HashMap() {{

            //TODO 数据源注册还可以继续优化
            put(SearchTypeEnum.POST.getValue(), esPostDataSource);
            put(SearchTypeEnum.USER.getValue(), sqlUserDataSource);
            put(SearchTypeEnum.PICTURE.getValue(), bingPictureDataSource);
        }};
    }

    public DataSource<?> getDataSourceByType(String type) {
        if (typeDataSourceMap == null) {
            return null;
        }
        return typeDataSourceMap.get(type);
    }
}
